/**
 * @file      nr_micro_shell_commands.c
 * @author    Nrush
 * @version   V0.1
 * @date      28 Oct 2019
 * *****************************************************************************
 * @attention
 * 
 * MIT License
 * 
 * Copyright (C) 2019 Nrush. or its affiliates.  All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Includes ------------------------------------------------------------------*/

#include "nr_micro_shell.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "tcpci.h"
#include "elog_flash.h"
#include "tcpm.h"
#include "pd_core.h"

#if SHELL_CLI_ENALBE

extern void test_elog(void);
extern struct sgm41620_tcpc *p_tcpc_dev;
void tcpc_enable_timer(struct sgm41620_tcpc *tcpc, uint32_t timer_id);
/**
 * @brief ls command
 */
void shell_ls_cmd(char argc, char *argv)
{
	unsigned int i = 0;
	if (argc > 1)
	{
		if (!strcmp("cmd", &argv[argv[1]]))
		{

			for (i = 0; nr_shell.static_cmd[i].fp != NULL; i++)
			{
				shell_printf("%s",nr_shell.static_cmd[i].cmd);
				shell_printf("\r\n");
			}
		}
		else if (!strcmp("-v", &argv[argv[1]]))
		{
			shell_printf("ls version 1.0.\r\n");
		}
		else if (!strcmp("-h", &argv[argv[1]]))
		{
			shell_printf("useage: ls [options]\r\n");
			shell_printf("options: \r\n");
			shell_printf("\t -h \t: show help\r\n");
			shell_printf("\t -v \t: show version\r\n");
			shell_printf("\t cmd \t: show all commands\r\n");
		}
	}
	else
	{
		shell_printf("ls need more arguments!\r\n");
	}
}


extern int tcpci_alert_fault(void);
#if CHIP_TYPE == CHIP_SG41620
extern int sgmdev_pd_read_reg(uint8_t reg, uint8_t len, uint8_t *dst);
extern int sgmdev_pd_write_reg(uint8_t reg, uint8_t len, uint8_t *src);
#endif
/**
 * @brief test command
 */
void shell_test_cmd(char argc, char *argv)
{
	unsigned int i;
	shell_printf("test command:\r\n");
	for (i = 0; i < argc; i++)
	{
		shell_printf("paras %d: %s\r\n", i, &(argv[argv[i]]));
	}
	
	int mode = atoi(&(argv[argv[1]]));
	int timer_id = atoi(&(argv[argv[2]]));
	switch(mode)
	{
#if defined(ELOG_MODULE_ENABLE) && ELOG_MODULE_ENABLE == 1
		case 1:
		{
			test_elog();
			break;
		}
		case 2:
		{
			elog_flash_clean();
			break;
		}
#endif // ELOG_MODULE_ENABLE == 1
		case 3:// test timer
		{
			tcpc_enable_timer(p_tcpc_dev, timer_id);
			break;
		}
		case 4:
		{
			break;
		}
		case 5:
		{
			tcpci_alert_fault();
			tcpci_alert_status_clear(p_tcpc_dev, 0xffffffff);
			break;
		}
        case 6:
        {
            uint8_t data_role;
            int ret;
            printf("---Test TCPM---\n");
            data_role = tcpm_inquire_pd_data_role(p_tcpc_dev);
            printf("Current Data Role is %s\n", data_role ? "DFP" : "UFP");
            ret = tcpm_dpm_pd_data_swap(p_tcpc_dev, data_role ? 0 : 1, NULL);
            if (ret == TCPM_SUCCESS) {
                printf("Request DR_Swap");
            } else {
                printf("return val %d\n", ret);
            }
            break;
        }
        case 7:
        {
            uint8_t data_role;
            data_role = tcpm_inquire_pd_data_role(p_tcpc_dev);
            printf("Data Role is %s after swap\n", data_role ? "DFP" : "UFP");
        }
		default:
		    break;
	}
	
}

/**
 * @brief read command
 */
//read all
void shell_read_cmd(char argc, char *argv)
{
	int i,size,ret = 0;
    uint8_t data[256];

	shell_printf("read command:\r\n");

	size = 0xA4 - 0x10 + 1;
    //memset(buf, '\0', sizeof(buf));
    for (i = 0;i < size /16;i++) {
#if CHIP_TYPE == CHIP_SG41620
        ret = sgmdev_pd_read_reg(0x10 + i *16, 16, &data[i *16 ]);
#endif
        if (ret < 0)
            return;
    }

    if (size % 16) {
#if CHIP_TYPE == CHIP_SG41620
        ret = sgmdev_pd_read_reg(0x10 + size - size % 16, size % 16, &data[size - size % 16]);
#endif
        if (ret < 0)
            return;
    }

    for (i = 0; i < size; i++) {
         printf("[REG_0x%.2x]=0x%.2x ",0x10 + i, data[i]);
    }
}


void shell_write_cmd(char argc, char *argv)
{
	unsigned int i;
	shell_printf("write command:\r\n");
	for (i = 0; i < argc; i++)
	{
		shell_printf("paras %d: %s\r\n", i, &(argv[argv[i]]));
	}

	int reg = atoi(&(argv[argv[1]]));
	uint8_t temp_data = atoi(&(argv[argv[2]]));
	sgmdev_pd_write_reg(reg,1,&temp_data);
	printf("write ok\r\n");
}

#ifdef NR_SHELL_USING_EXPORT_CMD
NR_SHELL_CMD_EXPORT(ls, shell_ls_cmd);
NR_SHELL_CMD_EXPORT(test, shell_test_cmd);
NR_SHELL_CMD_EXPORT(read, shell_read_cmd);
NR_SHELL_CMD_EXPORT(write, shell_write_cmd);
#else
const static_cmd_st static_cmd[] =
	{
		{"ls", shell_ls_cmd},
		{"test", shell_test_cmd},
		{"read", shell_read_cmd},
		{"write", shell_write_cmd},
		{"\0", NULL}};
#endif

/******************* (C) COPYRIGHT 2019 Nrush *****END OF FILE*****************/
#endif // SHELL_CLI_ENALBE

